﻿namespace KeRui.Upload
{
    using System;
    using System.Collections;
    using System.Web;

    internal class Settings
    {
        private AdvancedSettings mAdvanced = null;
        private NotesSettings mNotes = null;
        private QueryFieldsSettings mQueryFields = null;
        private static Settings sSettings = new Settings();

        private Settings()
        {
        }

        private static bool GetBool(IDictionary theConfig, string inKey, bool inDefault)
        {
            try
            {
                if (theConfig != null)
                {
                    inDefault = Convert.ToBoolean(theConfig[inKey].ToString());
                }
            }
            catch
            {
            }
            return inDefault;
        }

        private static int GetInt(IDictionary theConfig, string inKey, int inDefault)
        {
            try
            {
                if (theConfig != null)
                {
                    inDefault = Convert.ToInt32(theConfig[inKey].ToString());
                }
            }
            catch
            {
            }
            return inDefault;
        }

        private static string GetString(IDictionary theConfig, string inKey, string inDefault)
        {
            try
            {
                if (theConfig != null)
                {
                    inDefault = theConfig[inKey].ToString();
                }
            }
            catch
            {
            }
            return inDefault;
        }

        public AdvancedSettings Advanced
        {
            get
            {
                if (this.mAdvanced == null)
                {
                    this.mAdvanced = new AdvancedSettings();
                }
                return this.mAdvanced;
            }
        }

        public static Settings Current
        {
            get
            {
                return sSettings;
            }
        }

        public NotesSettings Notes
        {
            get
            {
                if (this.mNotes == null)
                {
                    this.mNotes = new NotesSettings();
                }
                return this.mNotes;
            }
        }

        public QueryFieldsSettings QueryFields
        {
            get
            {
                if (this.mQueryFields == null)
                {
                    this.mQueryFields = new QueryFieldsSettings();
                }
                return this.mQueryFields;
            }
        }

        public class AdvancedSettings
        {
            public int ChunkSize = 0x2000;
            public bool EnableLargeUploads = true;
            public bool EnableProgressBar = true;
            public bool FixCorruptUploads = true;
            public bool LogExceptions;
            public bool MultiThreaded = true;
            public bool Optimization = true;
            public string Serial = "";
            public string TempDirectory = "";

            public AdvancedSettings()
            {
                string name = "websupergoo.abcupload/advanced";
                IDictionary config = (IDictionary) HttpContext.Current.GetConfig(name);
                this.EnableProgressBar = Settings.GetBool(config, "enableProgressBar", this.EnableProgressBar);
                this.FixCorruptUploads = Settings.GetBool(config, "fixCorruptUploads", this.FixCorruptUploads);
                this.EnableLargeUploads = Settings.GetBool(config, "enableLargeUploads", this.EnableLargeUploads);
                this.MultiThreaded = Settings.GetBool(config, "multiThreaded", this.MultiThreaded);
                this.Optimization = Settings.GetBool(config, "optimization", this.Optimization);
                this.ChunkSize = Settings.GetInt(config, "chunkSize", this.ChunkSize);
                this.TempDirectory = Settings.GetString(config, "tempDirectory", this.TempDirectory);
                this.LogExceptions = Settings.GetBool(config, "logExceptions", this.LogExceptions);
                this.Serial = Settings.GetString(config, "serial", this.Serial);
            }
        }

        public class NotesSettings
        {
            public string CheckingSize = "Checking upload size...";
            public string Connecting = "Connecting...";
            public string Exception = "An error occurred. {0}";
            public string Finishing = "Finishing...";
            public string GettingSize = "Getting upload size...";
            public string Preloading = "Getting preloaded data...";
            public string Transferring = "Transferring data...";
            public string UploadStopped = "Upload stopped unexpectedly...";
            public string UploadTooLarge = "Upload size is greater than the maximum allowed...";

            public NotesSettings()
            {
                string name = "websupergoo.abcupload/notes";
                IDictionary config = (IDictionary) HttpContext.Current.GetConfig(name);
                this.Exception = Settings.GetString(config, "exception", this.Exception);
                this.Connecting = Settings.GetString(config, "connecting", this.Connecting);
                this.GettingSize = Settings.GetString(config, "gettingSize", this.GettingSize);
                this.CheckingSize = Settings.GetString(config, "checkingSize", this.CheckingSize);
                this.Preloading = Settings.GetString(config, "preloading", this.Preloading);
                this.Transferring = Settings.GetString(config, "transferring", this.Transferring);
                this.Finishing = Settings.GetString(config, "finishing", this.Finishing);
                this.UploadStopped = Settings.GetString(config, "uploadStopped", this.UploadStopped);
                this.UploadTooLarge = Settings.GetString(config, "uploadTooLarge", this.UploadTooLarge);
            }
        }

        public class QueryFieldsSettings
        {
            public string DumpPath = "";
            public string UploadID = "UploadID";

            public QueryFieldsSettings()
            {
                string name = "websupergoo.abcupload/queryfields";
                IDictionary config = (IDictionary) HttpContext.Current.GetConfig(name);
                this.UploadID = Settings.GetString(config, "uploadID", this.UploadID);
                this.DumpPath = Settings.GetString(config, "dumpPath", this.DumpPath);
            }
        }
    }
}

